
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CourseSelectionGUI extends JPanel implements ActionListener{
	
	private JComboBox courseBox;
	
	private JLabel courseLookup, selectLabel;
	private JButton firstLoginButton;
	private JPanel row1, row2, row3, row4, labelPanels;
	private String [] courseNames;
	
	public CourseSelectionGUI(){
		labelPanels = new JPanel();

		labelPanels.setLayout(new BoxLayout(labelPanels,BoxLayout.Y_AXIS));
		
		firstLoginButton = new JButton("Click Here if First Time Logging in");
		firstLoginButton.addActionListener(this);
		
		row1 = new JPanel(new FlowLayout());
		row2 = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		row4 = new JPanel(new FlowLayout());
		
		courseLookup = new JLabel("Course Lookup Page");
		selectLabel = new JLabel("Select a course:");
		
		row1.add(courseLookup);
		row2.add(firstLoginButton);
		row3.add(selectLabel);
		
		// all courses associated with the user logging in will be retrieved from database
		
		
		//String[] courses = {"", "Embedded Design", "VLSI Design", "Software Engineering"};
		
		courseBox = new JComboBox();
		courseBox.addItem("");
		courseBox.addActionListener(this);
		
		row4.add(courseBox);
		labelPanels.add(row1);
		labelPanels.add(row2);
		labelPanels.add(row3);
		labelPanels.add(row4);
		
		this.add(labelPanels, BorderLayout.NORTH);
	}
	
	public void setCourseNames()
	{
		// amm - this code would not work in the constructor 
		// because the coursedirectory isn't filled by the time this class is instantiated
		SystemController sc = GUIMaster.getWorkhorse();
		courseNames = new String[sc.courseDir.getCourses().length]; 
		Course c = null;
		int i;
		for (i = 0; i < courseNames.length; i++)
		{
			c = sc.courseDir.getCourses()[i];
			courseBox.addItem(c.getVerbose());
		}
	}
	
	public void actionPerformed(ActionEvent event){
		GUIMaster gm = GUIMaster.getInstance();
		SystemController sc = gm.getWorkhorse();
        if ("comboBoxChanged".equals(event.getActionCommand()))
        {
			if (courseBox.getSelectedIndex() != 0)
			{
				System.out.println(courseBox.getSelectedItem());
				Course c = gm.getWorkhorse().courseDir.getCourses()[courseBox.getSelectedIndex() - 1];
					
				boolean dateGood = false;
				if (dateGood && c.status == 2)
				{
					//get results for viewing (instructor only)
					Result [] r = sc.prepareResults(c.cid);
					gm.resultsGUI.setResults(r);
					gm.viewResults();
				}
				else
				{
					//previewing survey is set for student or instructor from setSurvey function
					//set survey page here
					
					gm.courseSurveyGUI.setSurvey(c.survey, c.status, c.getCid());
					
					//if statement set to false to force view results page
					// normally this conditional should depend on the time in the semester and the status
					
					gm.courseSelected();
				}
			}

        }
		else if(event.getSource() == firstLoginButton){
			gm.changePassRequest();
			System.out.println("moving to change pass page");
		}
	}
	
	/*public static void main(String arg[])
	{
		JFrame f = new JFrame();
		f.add(new CourseSelectionGUI());
		f.setSize(500, 500);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}*/
}
